package org.bot.client.flow;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.bot.api.flow.FlowJudgeType;
import org.bot.api.flow.FlowNode;
import org.bot.client.util.Bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 沧月
 * @create 0022 2023/9/22 09:18
 * @description <流程解析器>
 */

@Component
public class FlowConstructorV2 {
    static Logger logger = LoggerFactory.getLogger(FlowConstructorV2.class);

    public FlowNode buildFlowNode(JSONObject data, String flowId) {
        JSONArray flowJson = data.getJSONArray(FlowDataAttrFiled.FLOW_JSON.getKey());
        JSONObject flowAttr = data.getJSONObject(FlowDataAttrFiled.FLOW_ATTR.getKey());
        JSONObject flowLine = data.getJSONObject(FlowDataAttrFiled.FLOW_LINE.getKey());

        Map<String, FlowNode> flowMap = new HashMap<>();
        flowAttr.forEach((k, v) -> {
            FlowNode node = new FlowNode();
            node.setFlowId(flowId);
            this.setFlowNoteValue(node, (JSONObject) v);
            flowMap.put(k, node);
        });

        for (int i = 0; i < flowJson.size(); i++) {
            JSONObject _tmp = flowJson.getJSONObject(i);
            String source = _tmp.getString(FlowDataAttrFiled.SOURCE.getKey());
            String target = _tmp.getString(FlowDataAttrFiled.TARGET.getKey());
            String id = _tmp.getString(FlowDataAttrFiled.ID.getKey());
            String lineType = FlowJudgeType.YES.name();
            if (flowLine.containsKey(id)) {
                String type = flowLine.getJSONObject(id).getString(FlowDataAttrFiled.LINE_TYPE.getKey());
                lineType = FlowJudgeType.judgeType(type);
            }
            flowMap.get(source).getNextNote().put(lineType, flowMap.get(target));
        }
        for (String s : flowMap.keySet()) {
            if (flowMap.get(s).isRoot()) {
                return flowMap.get(s);
            }
        }
        throw new RuntimeException("未找到开始节点");
    }

    private void setFlowNoteValue(FlowNode node, JSONObject data) {
        logger.info("解析节点:: {}", data.getString(NodeAttrFiled.NAME.getValue()));
        FlowNodeCheck.checkEvent(data);
        node.setArg(data.getString(NodeAttrFiled.ARG.getValue()));
        node.setStaticValue(data.getString(NodeAttrFiled.STATIC_VALUE.getValue()));
        node.setSelectorType(data.getString(NodeAttrFiled.SELECT_TYPE.getValue()));
        node.setEvent(Bean.getEvents(data.getString(NodeAttrFiled.EVENT.getValue())));
        node.setDataIndex(data.getString(NodeAttrFiled.DATA_INDEX.getValue()));
        node.setFlowType(data.getString(NodeAttrFiled.FLOW_TYPE.getValue()));
        node.setName(data.getString(NodeAttrFiled.NAME.getValue()));
        node.setNodeId(data.getString(NodeAttrFiled.NODE_ID.getValue()));
        node.setWebWindowName(data.getString(NodeAttrFiled.WEB_WINDOW.getValue()));
        node.setRoot(data.getBooleanValue(NodeAttrFiled.IS_ROOT.getValue()));
        node.setDebug(data.getBooleanValue(NodeAttrFiled.DEBUG.getValue()));
    }
}
